﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    , m_paramTableUi(new ParamTableView(8,6,this))
{
    ui->setupUi(this);
    this->setWindowTitle(QStringLiteral("表格显示示例"));

    m_timer = new QTimer;
    m_timer->setInterval(1000);
    connect(m_timer, SIGNAL(timeout()), this, SLOT(refresh()));
    m_timer->start();


    m_paramTableUi->setData(0,0, "PID_kp");
    m_pidKpVal = 1.2;
    m_paramTableUi->setData(0,1, m_pidKpVal);

    m_paramTableUi->setData(1,0,"PID_ki");
    float pidKiVal = 0.12;
    m_paramTableUi->setData(1,1, pidKiVal);

    m_paramTableUi->setData(2,0,"PID_kd");
    float pidKdVal = 0.1;
    m_paramTableUi->setData(2,1, pidKdVal);

    m_paramTableUi->setData(3,0, "PID_out");
    float pidOut = 100;
    m_paramTableUi->setData(3,1,pidOut);

    m_paramTableUi->setData(0,2, QStringLiteral("机器人状态"));

    m_robotStat = new QStandardItem;
    m_robotStat->setText("normal");
    m_robotStat->setBackground(QBrush(Qt::blue));        // 设置背景色
    m_robotStat->setForeground(QBrush(Qt::white));         // 设置字体颜色
    m_paramTableUi->setData(0, 3, m_robotStat);

    this->setCentralWidget(m_paramTableUi);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::refresh()
{
    static bool isChange = false;
    if (!isChange)
    {
        m_robotStat->setText("error");
        m_robotStat->setBackground(QBrush(Qt::blue));        // 设置背景色
        m_robotStat->setForeground(QBrush(Qt::red));         // 设置字体颜色
        m_paramTableUi->setData(0, 3, m_robotStat);
        isChange = true;
    }
    else
    {
        m_robotStat->setText(" ");
        m_robotStat->setBackground(QBrush(Qt::blue));        // 设置背景色
        m_robotStat->setForeground(QBrush(Qt::white));         // 设置字体颜色
        m_paramTableUi->setData(0, 3, m_robotStat);
        isChange = false;
    }

    m_pidKpVal += 0.1;
    m_paramTableUi->setData(0,1, m_pidKpVal);
}

